<?php 
/**
 * crea una form
 *
 */
class elSimplyForm
{
	public $elSimplyForm;
	
	public function __construct($name, $el, $controller, $action= null, $method=null)
	{

		$this->elSimplyForm .= '<form name="'.$name.'" id="'.$name.'"'.(($action) ? ' action="'.$action.'" ' : '').' '.(($method) ? 'method="'.$action.'"' : '').' >'
		.$el
		.'</form>';
	}
}

class elStyleForm
{
	public $elStyleForm;
	
	public function __construct($label, $name, $el, $elButton, $controller = null, $action = null, $method = null)
	{
		$this->elStyleForm = '
		<div id="form_wrapper">
			<div id="form_head">'.$label.'</div>
			<div id="form_body">
				<div align="left">
					<form name="'.$name.'" id="'.$name.'"'.(($action) ? ' action="'.$action.'" ' : '').' '.(($method) ? ' method="'.$action.'" ' : '').' >'
						.$el
					.'</form>
				</div>
				<br>
				<div align="right">
					'.$elButton.'
				</div>
			</div>
		</div>';
	}
}

/**
 * Crea elementi html delle form 
 *
 */
class el
{
	/**
	 * Trasforma le opzioni array in opzioni stringa
	 *
	 * @param unknown_type $option
	 * @return unknown
	 */
	static private function optionToString($option)
	{
		foreach($option as $option=>$value)
		{
			if($value)
				$elOption .= ' '.$option.'='.$value.' ';
			else 
				$elOption .= $option;
		}
		return $elOption;
	}
	
	/**
	 * Aggiunge gli spazi &nbsp;
	 *
	 * @param unknown_type $space
	 * @return unknown
	 */
	static public function elS($space = 3)
	{
		for($i=0;$i<$space;$i++)
		{
			$elSpace .= '&nbsp;';
		}
		return $elSpace;
	}
	/**
	 * Aggiunge uno spazio
	 *
	 * @param unknown_type $crd
	 * @return unknown
	 */
	static public function elC($crd = 2)
	{
		for($i=0;$i<$crd;$i++)
		{
			$elCrd .= '<br>';
		}
		return $elCrd;
	}
	
	static public function elFieldValue($label, $value)
	{
		return "<b><strong>$label:</strong></b>".self::elS()."$value";
	}
	/**
	 * Aggiunge la label
	 *
	 * @param unknown_type $label
	 * @param unknown_type $name
	 * @return unknown
	 */
	static public function elLabel($label, $name)
	{
		return '<label for="'.$name.'">'.$label.'</label>';
	}
	
	/**
	 * Aggiunge un elemento input
	 *
	 * @param unknown_type $name
	 * @param unknown_type $type
	 * @param unknown_type $value
	 * @param unknown_type $option
	 * @return unknown
	 */
	static  public function elInput($name, $type, $value, $option=null)
	{
		return '<input type="'.$type.'" id="'.$name.'" name="'.$name.'" value="'.$value.'" '.(($option)?self::optionToString($option):'').' />';
	}
	
	/**
	 * Aggiunge un elemento Input Text
	 *
	 * @param unknown_type $label
	 * @param unknown_type $name
	 * @param unknown_type $value
	 * @param unknown_type $option
	 * @return unknown
	 */
	static public function elText($label, $name, $value=null, $option=null)
	{
		$elLabel = self::elLabel($label, $name);
		$elText = self::elInput($name,'text',$value, $option);
		return $elLabel.$elText;
	}
	
	/**
	 * Aggiunge un elemento input password
	 *
	 * @param unknown_type $label
	 * @param unknown_type $name
	 * @param unknown_type $value
	 * @param unknown_type $option
	 * @return unknown
	 */
	static public function elPassword($label, $name, $value=null, $option=null)
	{
		$elLabel = self::elLabel($label, $name);
		$elInput = self::elInput($name,'password',$value,$option);
		return $elLabel.$elInput;
	}

	static public function elHidden($name, $value=null)
	{
		return self::elInput($name, 'hidden', $value);
	}
	
	/**
	 * Aggiunge un elemento input Checkbox
	 *
	 * @param unknown_type $label
	 * @param unknown_type $name
	 * @param unknown_type $value
	 * @param unknown_type $option
	 * @return unknown
	 */
	static public function elCheck($label, $name, $value=null, $option=null)
	{
		$elLabel = self::elLabel($label, $name);
		$elInput = self::elInput($name,'checkbox',$value,$option);
		return $elLabel.$elInput;
	}
	
	/**
	 * Aggiunge un elemento input radio
	 *
	 * @param unknown_type $label
	 * @param unknown_type $name
	 * @param unknown_type $value
	 * @param unknown_type $option
	 * @return unknown
	 */
	static public function elRadio($label, $name, $value=null, $option=null)
	{
		$elLabel = self::elLabel($label, $name);
		$elInput = self::elInput($name,'radio',$value,$option);
		return $elLabel.$elInput;
	}
	
	/**
	 * Aggiunge un elemento combobox
	 *
	 * @param unknown_type $label
	 * @param unknown_type $name
	 * @param unknown_type $value
	 * @param unknown_type $option
	 * @return unknown
	 */
	static public function elSelect($label, $name, $value=null, $option=null)
	{
		$elLabel = self::elLabel($label, $name);
		$elSelect = '<select id="'.$name.'" name="'.$name.'" '.((is_array($option))?self::optionToString($option):'').'>';
		if(is_array($value))
			foreach($value as $val=>$opt)
			{
				$elSelect .= '<option value="'.$val.'">'.$opt.'</option>';	
			}
			
		$elSelect .= '</select>';
		
		return $elLabel.$elSelect;
	}
	
	static public function elFieldset($label, $content)
	{
		return '<fieldset><legend>'.$label.'</legend>'.$content.'</fieldset>'; 
	}
	
	
	static public function elButton($name, $value, $option=null)
	{
		return  '<input type="button" id="'.$name.'" name="'.$name.'" value="'.$value.'" '.((is_array($option))?self::optionToString($option):'').' />';
	}
	
	static public function elButtonReset($name, $value, $option=null)
	{
		return  '<input type="reset" id="'.$name.'" name="'.$name.'" value="'.$value.'" '.((is_array($option))?self::optionToString($option):'').' />';
	}
	
	static public function elTextArea($label, $name, $value = null, $option=null)
	{
		$elTextArea .= '<div><b>'.$label.'</b></div><textarea id="'.$name.'" name="'.$name.'" '.((is_array($option))?self::optionToString($option):'').'>'.$value.'</textarea>';
		return $elTextArea;
	}
	
	static public function elDiv($name, $content, $elOption = null)
	{
		return  '<div id="'.$name.'" '.((is_array($elOption))?self::optionToString($elOption):'').' >'.$content.'</div>';
	}
	
	static public function elDivBox($id, $name, $content, $closable = true)
	{
		if($closable)
			return '<div id="box-wrapper">
			<div id="'.$id.'-title" class="box-title"><span><a href="#" id="'.$id.'" name="'.$id.'"><img id="'.$id.'-img" border="0" src="images/up.gif" /></a></span>'.$name.'</div>
			<div id="'.$id.'-content" align="left" class="box-content">'.$content.'</div>
		</div>';
		else 
			return '<div id="box-wrapper">
			<div id="'.$id.'-title" class="box-title">'.$name.'</div>
			<div id="'.$id.'-content" align="left" class="box-content">'.$content.'</div>
		</div>';
		
	}
	

	
}




class elJquery
{
	static public function elJqueryTabs($name, $tabs, $content=null)
	{
		
		foreach ($tabs as $href=>$title)
		{
			$HeadTabs .= '<li><a href="'.$href.'">'.$title.'</a></li>';
		}
		if($content)
		{
			foreach ($content as $tabid=>$tabcontent)
			{
				$ContentTabs .= el::elDiv($tabid,$tabcontent);
			}
		}
		else 
			$ContentTabs = '';
		
		$HeadTabs = '<ul>'.$HeadTabs.'</ul>';
			
		return el::elDiv($name,$HeadTabs.$ContentTabs);
	}
	
	static public function elJqueryButton($label, $name, $selected = TRUE, $href='#', $icon=FALSE)
	{
		if($selected)
			return '<a id="'.$name.'" name="'.$name.'"  href="'.$href.'" >'.$label.'</a>
			<script type="text/javascript">
				$(function(){
					$("#'.$name.'").button('.(($icon)?'{icons:{primary:\'ui-icon-locked\'}}':'').')
				});
			</script>
			
			
			';
		else 
			return '<a id="'.$name.'" name="'.$name.'"  href="'.$href.'">'.$label.'</a>
			<script type="text/javascript">
				$(function(){
					$("#'.$name.'").button({'.(($icon)?'icons:{primary:\'ui-icon-locked\'},':'').'disabled:true})
				});
			</script>
			';
	}
	
	static public function elJquerySlider($type, $name, $toinputvalue, $option)
	{
		$SliderDiv = el::elDiv($name,'','');
		$Slider = '	<script type="text/javascript">
						$(function() {
							$("#'.$name.'").slider({
								range: "'.$type.'",
								'.elJquery::elJqueryArrayToStringOption($option).'
								slide: function(event, ui){
									'.$toinputvalue.';
								}
							})	
						});
					</script>
		';
		return $SliderDiv.$Slider;
	}
	
	
	static private function elJqueryArrayToStringOption($optionarray)
	{
			foreach($optionarray as $Option=>$Value)
			{
				$StringOption .= ''.$Option.': '.$Value.',';
			}
			return $StringOption;
	}
	
}


class elSimplyGrid
{
	public $elSimplyGrid;
	public $elSimplyDataGrid;
	public $elGridDataOption;
	public $elGridStaticOption;
	
	public function createSimplyGrid($label, $name, $head, $data, $elButton, $option=false, $datatype='array')
	{
		$elDataGrid = '<table width="100%" id="tbl_'.$name.'" class="simplyTable" cellpadding="0" cellspacing="0" border="0"><thead><tr>';
		foreach($head as $headname)
		{
			$elDataGrid .= '<th>'.$headname.'</th>';
		}
		if($option)
		{
			$elDataGrid .= '<th colspan="'.$option.'">Opzioni</th>';
		}
		$elDataGrid .= '</tr></thead>';
		$elDataGrid .= '<tbody>';
		$class_row = 'row_b';
		switch ($datatype)
		{
			case 'array':
				foreach ($data as $row)
				{
					
					$elDataGrid .= '<tr class="'.(($class_row == 'row_a')? 'row_a':'row_b').'" id="'.$row['id'].'">';
					$class_row = ($class_row == 'row_b')? 'row_a':'row_b';
					foreach($row as $valname=>$val)
					{		
						if(preg_match("/option_/i", $valname) and $option)
						{
							$elDataGrid .= '<td> '.$this->returnStaticOption($valname, $val).'</td>';
						}
						elseif($valname != 'id')
							$elDataGrid .= '<td>'.$this->returnDataOption($valname, $val).'</td>';
					}

					$elDataGrid .= '</tr>';
				}
			break;
		}
		$elDataGrid .= '</tbody>';
		$elDataGrid .= '</table>';
		$this->elSimplyDataGrid = $elDataGrid;
		
		$elGrid .= '<div id="table_wrapper"><div id="table_head">'.$label.'</div><div id="'.$name.'">';
		$elGrid .= $elDataGrid;
		$elGrid .= '<div id="table_button" align="right">'.$elButton.'</div>';
		$elGrid .= '</div></div>';
		
		$this->elSimplyGrid = $elGrid;
	}
	
	public function setDataOption($name, $type)
	{
		$this->elGridDataOption[$name] = $type;
	}
	
	public function setStaticOption($optioniftrue, $optioniffalse, $optiontype, $optioname)
	{
		$this->elGridStaticOption[$optioname]['iftrue'] = $optioniftrue;
		$this->elGridStaticOption[$optioname]['iffalse'] = $optioniffalse;
		$this->elGridStaticOption[$optioname]['type'] = $optiontype;
	}
	
	private function returnStaticOption($optioname, $valoption)
	{
		switch($this->elGridStaticOption[$optioname]['type'])
		{
			case "istrue":
				if($valoption == 1)
					return $this->elGridStaticOption[$optioname]['iftrue'];
				else
					return $this->elGridStaticOption[$optioname]['iffalse'];
			break;
			case "istexteval":
				if($valoption != null )
				{
					$title = $valoption;
					$str = $this->elGridStaticOption[$optioname]['iftrue'];
					eval("\$str = \"$str\";");
					return $str;
										
				}
				else
				{
					$str = $this->elGridStaticOption[$optioname]['iffalse'];
					eval("\$str = \"$str\";");
					return $str;
				}
			break;
			
		}
	}
	
	private function returnDataOption($row, $value)
	{
		switch ($this->elGridDataOption[$row])
		{
			case "bool-yes-no":
				if($value){return "SI";}
				else{return "NO";}
			break;
			case "long-time":
					return sys::getdate($value,'long');
			break;
			case "combo_list_action":
					return el::elSelect("",$row,$value,null).el::elS().el::elButton("act",">");
			break;
			case "combo_list":
					return el::elSelect("",$row,$value,null);
			break;
			default:
				return $value;
			break;
		}
		
	}
	
	private function setStaticFilter()
	{
		
	}
	
	Private Function setStaticOrder()
	{
		
	}
}

class elSimplyCalendar extends calendar_data 
{
	
	public $mesi = Array(1=>"Gennaio", 2=>"Febbraio", 3=>"Marzo", 4=>"Aprile", 5=>"Maggio", 6=>"Giugno", 7=>"Luglio", 8=>"Agosto", 9=>"Settembre", 10=>"Ottobre", 11=>"Novembre", 12=>"Dicembre");
	public $elCalendar;
	protected $questoMese;
	protected $questoAnno;
	protected $successivoMese;
	protected $precedenteMese;
	protected $successivoAnno;
	protected $precedenteAnno;
	protected $dataInDay;
	protected $ExternalLink;
	public function __construct($Link)
	{
		parent::__construct();
		
		$this->ExternalLink = $Link;
		
		if(isset($_REQUEST['questomese']))
			$this->questoMese = $_REQUEST['questomese'];
		else 
			$this->questoMese = date('n');
			
		if(isset($_REQUEST['questoanno']))
			$this->questoAnno = $_REQUEST['questoanno'];
		else 
			$this->questoAnno = date('Y');
		
		$this->precedenteMese = $this->questoMese-1;
		$this->successivoMese = $this->questoMese+1;
		$this->successivoAnno = $this->questoAnno;
		$this->precedenteAnno = $this->questoAnno;

		if ($this->precedenteMese == 0 ) {
			$this->precedenteMese = 12;
			$this->precedenteAnno = $this->questoAnno - 1;
		}

		if ($this->successivoMese == 13 ) {
			$this->successivoMese = 1;
			$this->successivoAnno = $this->questoAnno + 1;
		}
		
		$MeseCorrente = mktime(0,0,1,$this->questoMese, 1, $this->questoAnno);
		$this->setDataInDay(parent::getCalendarEvents(mktime(0,0,1,$this->questoMese, 1, $this->questoAnno), mktime(23,59,59,$this->questoMese, date('t',$MeseCorrente), $this->questoAnno)));
		
		
	}
	
	private function setDataInDay($Content)
	{
		if(is_array($Content))
		{
			foreach($Content as $el)
			{
				$this->dataInDay[$el['date']] .= '<li id="'.$el['id'].'" class="fix"><a href="'.$this->ExternalLink.'&id='.$el['id'].'"<strong>'.$el['time'].'&nbsp;</strong>'.$el['title'].'</a></li>';
			}
		}
		
		
		$this->dataInDay[$data] = $Content;
	}
	
	
	public function createCalendar()
	{
		$this->elCalendar .= '<div id="calendar_wrapper"><table aligh="center" id="calendar"  border="0" cellpadding="2" cellspacing="0">'
		.'<tr  class="head_month"><td style="border-right: 0px;" colspan=2><a style="color: black;" href="index.php?do=calendar&questomese='.($this->precedenteMese).'&questoanno='.$this->precedenteAnno.'"><b><<</b></a></td><td style="border-right: 0px;" align="center" colspan="3"><b>'.date('F',mktime(0,0,0,$this->questoMese, 1, $this->questoAnno)).'&nbsp;&nbsp;&nbsp;'.$this->questoAnno.'</b></td><td  align="right" colspan="2"><a style="color: black;" href="index.php?do=calendar&questomese='.($this->successivoMese).'&questoanno='.$this->successivoAnno.'"><b>>></b></a></td></tr>'
		.'<thead><tr>'
		.'<td width="110px" height="20px"><div id="dayHead">Lunedi</div></td>'
		.'<td width="110px" height="20px"><div id="dayHead">Martedi</div></td>'
		.'<td width="110px" height="20px"><div id="dayHead">Mercoledi</div></td>'
		.'<td width="110px" height="20px"><div id="dayHead">Giovedi</div></td>'
		.'<td width="110px" height="20px"><div id="dayHead">Venerdi</div></td>'
		.'<td width="110px" height="20px"><div id="dayHead">Sabato</div></td>'
		.'<td width="110px" height="20px"><div id="dayHead">Domenica</div></td>'
		.'</tr></thead><tbody>';
		
		$MeseCorrente = mktime(0,0,0,$this->questoMese, 1, $this->questoAnno);
		$MesePrecedente = mktime(0,0,0,$this->questoMese - 1, 1, $this->questoAnno);
		$MeseSuccessivo = mktime(0,0,0,$this->questoMese + 1, 1, $this->questoAnno);
		
		$GiorniInMeseCorrente = date('t',$MeseCorrente);
		$GiorniInMesePrecedente = date('t',$MesePrecedente);
		$GiorniInMeseSuccessivo = date('t',$MeseSuccessivo);
		
		$InfoMeseCorrente = getdate($MeseCorrente);
		$InfoMesePrecedente = getdate($MesePrecedente);
		$InfoMeseSuccessivo = getdate($MeseSuccessivo);

		$PrimoGiornoMeseCorrente = $InfoMeseCorrente['wday'];
		if($PrimoGiornoMeseCorrente == 0){
			 $PrimoGiornoMeseCorrente = 7;
		}
		$GiorniRimanentiPrimaSettimana = 6 - (7 - $PrimoGiornoMeseCorrente) ;
		$GiorniRimanentiMesePrecedente = $GiorniInMesePrecedente - ($GiorniInMesePrecedente - $GiorniRimanentiPrimaSettimana);
		$GiorniRimanentiMeseSuccessivo = $GiorniRimanentiMesePrecedente + $GiorniInMeseCorrente;
		
		for ($i=1; $i!=43; $i++)
		{
			
			if(($i % 7) == 1 ) $this->elCalendar .= "<tr>\n";
			if($i <= $GiorniRimanentiMesePrecedente){$this->elCalendar .= '<td id="'.($GiorniInMesePrecedente - $GiorniRimanentiMesePrecedente + $i).'-'.($this->questoMese - 1).'-'.$this->questoAnno.'" height="110px" valign="top"><div align="left" class="AltroMese">'.($GiorniInMesePrecedente - $GiorniRimanentiMesePrecedente + $i).'</div><div id="DayContent"></div></td>';}	
			elseif ($i > $GiorniRimanentiMeseSuccessivo){$this->elCalendar .= '<td id="'.($i - $GiorniRimanentiMeseSuccessivo).'-'.($this->questoMese + 1).'-'.$this->questoAnno.'" height="110px" valign="top"><div   align="left" class="AltroMese">'.($i - $GiorniRimanentiMeseSuccessivo).'</div><div id="DayContent"></div></td>';}	
			else
			{
				$this->elCalendar .= '<td id="'.($i - $GiorniRimanentiMesePrecedente).'-'.($this->questoMese).'-'.$this->questoAnno.'" height="110px" valign="top">'
				.'<div align="left" class="QuestoMese">'.($i - $GiorniRimanentiMesePrecedente).'</div>';
				$this->elCalendar .= '<div id="DayContent"><ul>';
				
				if(isset($this->dataInDay[($i - $GiorniRimanentiMesePrecedente).'-'.($this->questoMese).'-'.$this->questoAnno]))
				{
					$this->elCalendar .= $this->dataInDay[($i - $GiorniRimanentiMesePrecedente).'-'.($this->questoMese).'-'.$this->questoAnno];
				}
				
				
				$this->elCalendar .= '</ul></div>';
				$this->elCalendar .= '</td>';

				
			}	
				
				
			if(($i % 7) == 0 ) $this->elCalendar .= "</tr>\n";
		}
		
		$this->elCalendar .= '<tbody></table></div>';  
	}
}
?>